home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tvx.arc / TVX-UNIX.C < prev   
C/C++ Source or Header  |  1989-07-30  |  14KB  |  583 lines

  1. /* -------------------------- tvx_unix.c ------------------------------ */
  2. #include "tvx_defs.ic"
  3. #include "tvx_glbl.ic"
  4.  
  5. #define TEMPEXT ".$$1"        /* temporary file */
  6. #define BACKEXT ".B"        /* backup file */
  7. #define SWITCH '-'
  8. #define FILESEP '.'
  9.  
  10. /* define USETMP if you want intermediate workfiles built on
  11.    /tmp.  Otherwise, they will be built on the same directory as
  12.    the original file.  This latter method is often a bit faster,
  13.    especially when exiting if /tmp is on a different volume than
  14.    the destination file (which makes mv COPY the file rather than
  15.    just renameing. */
  16.  
  17. /* #define USETMP */            /* define if create temp files on /tmp */
  18.  
  19. #include <ctype.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/types.h>
  22.  
  23.  
  24. /* --------------  terminal I/O stuff --------------- */
  25.  
  26. static struct sgttyb sgb;
  27. static struct tchars tch;
  28. static struct ltchars ltc;
  29.  
  30. #define Ioctl ioctl
  31. #define Read read
  32. #define Write write
  33.  
  34. /* ------------- file mode stuff ---------------------- */
  35. #include <sys/stat.h>
  36.   static struct stat info;        /* structure to get info */
  37.  
  38. /* ------------- misc stuff ---------------------- */
  39.  
  40.   extern int errno;
  41.   extern char **environ;
  42.  
  43.  
  44. #ifdef TERMCAP            /* routines needed for termcap */
  45. /* ------------- termcap stuff ---------------------- */
  46.   char PC;
  47.   char *BC;
  48.   char *UP;
  49.   char TERM[40];
  50.   short ospeed;
  51.  
  52.   static char Tcm[80];        /* special entry for cm */
  53.   static char empty[2];
  54.   static char Tbc[20];
  55.   static char Tup[20];
  56.  
  57.   static int    Tco,            /* number of columns per line */
  58.         Tli;            /* number of lines */
  59.  
  60.   static char tcbuff[1024];        /* buffer to hold termcap entry */
  61.  
  62.  
  63. /* ==========================>>> gettermcap  <<<========================= */
  64.   gettermcap()
  65.   {
  66.     char *tp;
  67.     char *getenv();
  68.     char entry[80];        /* scratch buffer for entry */
  69.  
  70.     empty[0] = 0;
  71.  
  72.     ospeed = sgb.sg_ospeed;    /* get the speed */
  73.  
  74.     if ((tp = getenv("TERM")) == NULL)
  75.       {
  76.     goto FORCETTY;
  77.       }
  78.     strcpy(TERM,tp);        /* copy to our TERM */
  79.  
  80.     if (tgetent(tcbuff,TERM) < 1)
  81.       {
  82.     goto FORCETTY;
  83.       }
  84.  
  85. /*    read required termcap entries, save in appropriate TVX arrays */
  86.  
  87.     if (!gettcap("cm",Tcm))
  88.       {
  89.     goto FORCETTY;
  90.       }
  91.  
  92.     if (!gettcap("ce",entry))
  93.       {
  94.     goto FORCETTY;
  95.       }
  96.     if (!capcpy(celin,entry,7))    /* copy entry to end of line */
  97.       {
  98.     goto FORCETTY;
  99.       }
  100.     
  101.     gettcap("cd",entry);        /* clear to end of display */
  102.     capcpy(cescr,entry,7);
  103.  
  104.     gettcap("al",entry);        /* insert a line (add line) */
  105.     capcpy(ciline,entry,7);
  106.  
  107.     gettcap("dl",entry);    /* delete a line */
  108.     capcpy(ckline,entry,7);
  109.  
  110.     if (!gettcap("sr",entry))    /* reverse scroll */
  111.       {
  112.     strcpy(ctopb,ciline);    /* add line works the same */
  113.       }
  114.     else
  115.     capcpy(ctopb,entry,7);
  116.  
  117.     gettcap("ve",entry);        /* stand cursor changer end */
  118.     capcpy(ccsrcm,entry,7);
  119.     gettcap("vs",entry);        /* stand cursor changer begin */
  120.     capcpy(ccsrin,entry,7);
  121.  
  122.     gettcap("se",entry);        /* stand out end */
  123.     capcpy(cbolde,entry,7);
  124.  
  125.     gettcap("so",entry);        /* begin standout */
  126.     capcpy(cboldb,entry,7);
  127.  
  128.     cerred[0] = 7;                /* bell for sure */
  129.     gettcap("vb",entry);        /* visual bell? */
  130.     if (*entry)
  131.     capcpy(cerred,entry,7);
  132.  
  133.     if (!capcpy(&cversn[1],TERM,10))        /* copy name to version */
  134.     strcpy(cversn,"TERMCAP");
  135.  
  136.     if ((Tco = tgetnum("co")) < 0)    /* # of cols */
  137.     Tco = 79;        /* assume 80 x 24 */
  138.     if ((Tli = tgetnum("li")) < 0)    /* # of lines */
  139.     Tli = 24;        /* assume 80 x 24 */
  140.  
  141.     tvhardlines = tvlins = Tli;    /* number of lines */
  142.     tvcols = Tco - 1; /* set col val (-1 avoids all the line wrap crap )*/
  143.     if (tvhardlines != 24 || tvhardlines != 25)    /* strange terminal */
  144.       {
  145.     ddline = (tvlins / 2) + 1;
  146.     setdscrl();        /* calculate scroll */
  147.       }
  148.  
  149.     gettcap("bc",entry);        /* get backspace character */
  150.     if (!*entry)
  151.       {
  152.     Tbc[0] = 8; Tbc[1] = 0;
  153.       }
  154.     else
  155.     capcpy(Tbc,entry,19);
  156.     BC = Tbc;
  157.     gettcap("up",entry);        /* get backspace character */
  158.     if (!*entry)
  159.       {
  160.     Tup[0] = 0;
  161.       }
  162.     else
  163.     capcpy(Tup,entry,19);
  164.     UP = Tup;
  165.     gettcap("pc",entry);        /* get the pad character */
  166.     PC = *entry;
  167.  
  168.     gettcap("is",entry);        /* initialization string */
  169.     tcapcs(entry);            /* send the intialization string */
  170.  
  171.     gettcap("ti",entry);        /* cm initialization string */
  172.     tcapcs(entry);            /* send the intialization string */
  173.  
  174.     return;
  175.  
  176. FORCETTY:
  177.    force_tty = TRUE;
  178.    remark("Unable to set up for video terminal, tty mode assumed.");
  179.    strcpy(cversn,"tty");
  180.   }
  181.  
  182. /* =============================>>> capcpy  <<<============================= */
  183.   capcpy(to,from,len)
  184.   char *to, *from;
  185.   int len;
  186.   {        /* copy a capability, checking length */
  187.     if (strlen(from) > len)
  188.       {
  189.     *to = 0;
  190.     return (FALSE);
  191.       }
  192.     else
  193.     strcpy(to,from);
  194.     return (TRUE);
  195.   }
  196.  
  197. /* =============================>>> gettcap  <<<============================= */
  198.   gettcap(cap,area)
  199.   char *cap, *area;
  200.   {
  201.     char **cpp, *cp;
  202.  
  203.     cpp = &cp;        /* I think */
  204.     cp = area;
  205.     *area = 0;        /* assume null entry */
  206.  
  207.     tgetstr(cap,cpp);    /* get the capability */
  208.     return (*area);        /* return 1st char */
  209.     
  210.   }
  211.  
  212. /* =============================>>> tcapcs  <<<============================= */
  213.   tcapcs(str)
  214.   char *str;
  215.   {
  216.      /* send a termcap generated control string to terminal */
  217.  
  218.     register char *cp;
  219.     int ttwt();
  220.  
  221.     if (!(echof && !bakflg && !ttymode))
  222.     return;
  223.     if (!*str)        /* don't send null strings */
  224.     return;
  225.     cp = str;
  226.     tputs(cp,1,ttwt);
  227.  
  228.   }
  229.  
  230. /* =============================>>> tcapxy  <<<============================= */
  231.   tcapxy(x,y)
  232.   int x,y;
  233.   {
  234.     /* move cursor to x,y */
  235.  
  236.    char *tgoto();
  237.  
  238.    tcapcs(tgoto(Tcm,x-1,y-1));    /* send the string, adjusting x,y */
  239.  
  240.   }
  241. #endif   /* termcap */
  242.  
  243.  
  244. /* =============================>>> ttinit  <<<============================= */
  245.   ttinit()
  246.   {
  247.     struct sgttyb nsgb;
  248.     struct tchars ntch;
  249.     struct ltchars nltc;
  250.  
  251.     (void) Ioctl(0, TIOCGETP, &sgb);
  252.     (void) Ioctl(0, TIOCGETP, &nsgb);
  253.     (void) Ioctl(0, TIOCGETC, &tch);
  254.     (void) Ioctl(0, TIOCGETC, &ntch);
  255.     (void) Ioctl(0, TIOCGLTC, <c);
  256.  
  257.     nsgb.sg_flags |= CBREAK;
  258.     nsgb.sg_flags &= ~(CRMOD|ECHO|LCASE|TANDEM);
  259.  
  260.     ntch.t_intrc = -1;  /* interrupt */
  261.     ntch.t_quitc = -1;  /* quit */
  262.  
  263. /* the following two lines control flow control */
  264.  
  265. #ifndef FLOWCONTROL
  266.     ntch.t_startc = -1; /* start output */
  267.     ntch.t_stopc = -1;  /* stop output */
  268. #endif
  269.  
  270.     ntch.t_eofc = -1;   /* end-of-file */
  271.     ntch.t_brkc = -1;   /* input delimiter (like nl) */
  272.  
  273.     nltc.t_suspc = -1;  /* stop process signal */
  274.     nltc.t_dsuspc = -1; /* delayed stop process signal */
  275.     nltc.t_rprntc = -1; /* reprint line */
  276.     nltc.t_flushc = -1; /* flush output (toggles) */
  277.     nltc.t_werasc = -1; /* word erase */
  278.     nltc.t_lnextc = -1; /* literal next character */
  279.  
  280.     (void) Ioctl(0, TIOCSETP, &nsgb);
  281.     (void) Ioctl(0, TIOCSETC, &ntch);
  282.     (void) Ioctl(0, TIOCSLTC, &nltc);
  283.  
  284. #ifdef TERMCAP
  285.     gettermcap();            /* set up terminal characteristics */
  286. #endif
  287.  
  288.     info.st_mode = -1;            /* no mode stuff yet */
  289.  }
  290.  
  291.  
  292. /* =============================>>> ttrd_unix  <<<============================= */
  293.   ttrd_unix()
  294.   {
  295.            char c;
  296.  
  297.     Read(0, &c, 1);
  298.     return(c);
  299.   }
  300.  
  301. /* =============================>>> ttwtln  <<<============================= */
  302.   ttwtln(cbuf,cnt)
  303.   char *cbuf;
  304.   int cnt;
  305.   {
  306.     if (echof && !bakflg && !ttymode)
  307.     Write(1, cbuf, cnt);
  308.   }
  309.  
  310. /* =============================>>> ttwt  <<<============================= */
  311.   ttwt(c)
  312.   char c;
  313.   {
  314.     if (ttymode)
  315.     return;
  316.     Write(1, &c, 1);
  317.   }
  318.  
  319. /* =============================>>> ttclos  <<<============================= */
  320.   ttclos()
  321.   {
  322.  
  323. #ifdef TERMCAP
  324.     char entry[80];
  325.  
  326.     gettcap("te",entry);        /* cm end up string */
  327.     tcapcs(entry);            /* send it */
  328.  
  329. #endif
  330.     (void) Ioctl(0, TIOCSETP, &sgb);
  331.     (void) Ioctl(0, TIOCSETC, &tch);
  332.     (void) Ioctl(0, TIOCSLTC, <c);
  333.   }
  334.  
  335. /* =============================>>> ttosinit  <<<============================= */
  336.   ttosinit()
  337.   {            /* need a special version for not doing termcap */
  338.     struct sgttyb nsgb;
  339.     struct tchars ntch;
  340.     struct ltchars nltc;
  341.     char entry[80];        /* scratch buffer for entry */
  342.  
  343.     (void) Ioctl(0, TIOCGETP, &sgb);
  344.     (void) Ioctl(0, TIOCGETP, &nsgb);
  345.     (void) Ioctl(0, TIOCGETC, &tch);
  346.     (void) Ioctl(0, TIOCGETC, &ntch);
  347.     (void) Ioctl(0, TIOCGLTC, <c);
  348.  
  349.     nsgb.sg_flags |= CBREAK;
  350.     nsgb.sg_flags &= ~(CRMOD|ECHO|LCASE|TANDEM);
  351.  
  352.     ntch.t_intrc = -1;  /* interrupt */
  353.     ntch.t_quitc = -1;  /* quit */
  354.  
  355. /* the following two lines control flow control */
  356.  
  357. #ifndef FLOWCONTROL
  358.     ntch.t_startc = -1; /* start output */
  359.     ntch.t_stopc = -1;  /* stop output */
  360. #endif
  361.  
  362.     ntch.t_eofc = -1;   /* end-of-file */
  363.     ntch.t_brkc = -1;   /* input delimiter (like nl) */
  364.  
  365.     nltc.t_suspc = -1;  /* stop process signal */
  366.     nltc.t_dsuspc = -1; /* delayed stop process signal */
  367.     nltc.t_rprntc = -1; /* reprint line */
  368.     nltc.t_flushc = -1; /* flush output (toggles) */
  369.     nltc.t_werasc = -1; /* word erase */
  370.     nltc.t_lnextc = -1; /* literal next character */
  371.  
  372.     (void) Ioctl(0, TIOCSETP, &nsgb);
  373.     (void) Ioctl(0, TIOCSETC, &ntch);
  374.     (void) Ioctl(0, TIOCSLTC, &nltc);
  375.  
  376. #ifdef TERMCAP
  377.     gettcap("is",entry);        /* initialization string */
  378.     tcapcs(entry);            /* send the intialization string */
  379.  
  380.     gettcap("ti",entry);        /* cm initialization string */
  381.     tcapcs(entry);            /* send the intialization string */
  382. #endif
  383.  }
  384.  
  385. /* ==========================>>> unix_sys  <<<============================= */
  386.   unix_sys()
  387.   {
  388.     char rp[150];
  389.     int oldtty;
  390.  
  391.     tvclr();            /* clear the screen */
  392.     oldtty = ttymode; ttymode = FALSE;
  393. DO_UNIX:
  394.     remark("Unix command interface"); remark("");
  395.     remark("Enter Unix command line: ");
  396.     reply(rp,149);
  397.     reset();        /* reset terminal to unix mode */
  398.  
  399.     system(rp);
  400.  
  401.     ttosinit();        /* reset terinal to our mode */
  402.  
  403.     remark("");
  404.     remark("");
  405.  
  406.     prompt("Any key to continue with edit (! for another Unix command): ");
  407.     reply(rp,1);
  408.  
  409.     trmini();        /* this has to be here or screen is cleared! */
  410.     if (*rp == '!')
  411.     goto DO_UNIX;
  412.  
  413.     ttymode = oldtty;
  414.     verify(1);
  415.   }
  416.  
  417. /* =============================>>> get_mode <<<============================= */
  418.   get_mode(f)
  419.   FILE *f;
  420.   {        /* gets access mode of open file f */
  421.  
  422.     char rp[10];
  423.  
  424.     info.st_mode = -1;    /* assume no mode */
  425.  
  426.     if (newfil)
  427.     return;
  428.     if (fstat(fileno(f),&info) != 0)
  429.       {
  430.     info.st_mode = -1;    /* assume no mode */
  431.     return;
  432.       }
  433.     info.st_mode &= 07777;    /* zap extraneous stuff*/
  434.     if (((info.st_mode & 0222) == 0) && !rdonly)  /* no write permission */
  435.       {
  436.     prompt("No write permission for file, edit R/O? (y/n) ");
  437.     ureply(rp,1);
  438.     if (*rp == 'Y')
  439.         rdonly = TRUE;
  440.     else
  441.       {
  442.         reset();
  443.         exit(999);
  444.       }
  445.       }
  446.   }
  447.  
  448. /* =============================>>> set_mode <<<============================= */
  449.   set_mode(f)
  450.   FILE *f;
  451.   {        /* sets access mode of open file f */
  452.     if (newfil || info.st_mode == -1)
  453.     return;
  454.     if (fchmod(fileno(f),info.st_mode) != 0)
  455.     tverrb("Unable to set file mode, umask will be used.");
  456.   }
  457.  
  458. /* ==========================>>> expand_name <<<============================ */
  459.   expand_name(n)
  460.   char *n;
  461.   {        /* expands unix file names */
  462.     char tmp[FNAMESIZE+1];
  463.  
  464.     if ((*n == '~') && (n[1] == '/'))
  465.       {
  466.     strcpy(tmp,getenv("HOME"));
  467.     scopy(n,1,tmp,strlen(tmp));
  468.     strcpy(n,tmp);
  469.       }
  470.   }
  471.  
  472. /* =============================>>> ren_file <<<=========================== */
  473.   ren_file(old,new)
  474.   char *old, *new;
  475.   {
  476.     int pid;
  477.     static char *mvarg[4];
  478.     static int status;
  479.  
  480.     if (rename(old,new) != 0)
  481.       {
  482.     mvarg[0] = "/bin/mv";
  483.     mvarg[1] = old;
  484.     mvarg[2] = new;
  485.     mvarg[3]=0;
  486.     pid=fork();
  487.     if (pid == 0)
  488.       {
  489.         execve("/bin/mv",mvarg,environ);
  490.             tverrb("Error trying to start mv utility");
  491.         _exit(999);
  492.       }
  493.     wait(&status);
  494.     if (status > 255)        /* error return */
  495.       {
  496.         prompt(old) ; prompt(" not renamed to "); remark(new);
  497.         prompt("Edited file found as: "); remark(old);
  498.       }
  499.       }
  500.   }
  501.  
  502. /* =============================>>> temp_name <<<=========================== */
  503.   temp_name(n,first)
  504.   char *n;
  505.   int first;
  506.   {
  507.     /* generates a temporary name from n.  Depending on value of
  508.        first, it will either add a 1 or 2 to name */
  509.     SLOW int i;
  510.  
  511. #ifdef USETMP
  512.     SLOW char pidno[20];
  513.     long pidint;
  514.  
  515.     if (first)            /* create full temp name */
  516.       {
  517.     *n = 0;
  518.     pidint=getpid();
  519.     itoa(pidint,pidno);
  520.     strcpy(n,"/tmp/tvx1");
  521.     scopy(pidno,0,n,9);
  522.       }
  523.     else            /* alternate between 1 and 2 */
  524.       {
  525.     if (n[8] == '1')
  526.         n[8] = '2';
  527.     else
  528.         n[8] = '1';
  529.       }
  530. #else
  531.     if (first)
  532.       {
  533.     if ((i = rindex(n,FILESEP)) > 0)    /* see if extenstion */
  534.         scopy(TEMPEXT,0,n,i);        /* make .bak */
  535.     else
  536.       {
  537.         scopy(TEMPEXT,0,n,strlen(n));    /* just add on */
  538.       }
  539.       }
  540.     else
  541.       {
  542.     i = strlen(n);
  543.     if (n[i-1] == '1')
  544.         n[i-1] = '2';
  545.     else
  546.         n[i-1] = '1';
  547.       }
  548. #endif
  549.   }
  550.  
  551. #ifndef SUN
  552. /* =============================>>> USER_1 <<<============================= */
  553.   user_1(knt)
  554.   int knt;
  555.   {
  556.     knt = 0;
  557.     return (TRUE);
  558.   }
  559.  
  560. #else
  561. /* =============================>>> USER_1 <<<============================= */
  562.   user_1(knt)
  563.   int knt;
  564.   {
  565.     /* for suns, re-initialize window */
  566. #ifdef TERMCAP
  567.     gettermcap();        /* set up terminal characteristics */
  568.     tvidefs();        /* and reset defs */
  569. #endif
  570.     verify(1);
  571.     return (TRUE);
  572.   }
  573. #endif
  574.  
  575. /* =============================>>> USER_2 <<<============================= */
  576.   user_2(knt)
  577.   int knt;
  578.   {
  579.     knt = 0;
  580.     return (TRUE);
  581.   }
  582. /* -------------------------- tvx_unix.c ------------------------------ */
  583.